package com.unionfetch.content_home.ui.step

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.unionfetch.content_home.R
import com.unionfetch.content_home.bean.UserStatus
import com.unionfetch.content_home.bean.UserType
import com.unionfetch.content_home.ui.OpenAccountGuideUtil
import com.unionfetch.content_home.ui.auth.IdentityAuthActivity
import com.unionfetch.content_home.ui.auth.SumsubManager
import com.unionftech.common.R.color
import com.unionftech.common.R.drawable
import com.unionftech.common.R.string
import com.unionftech.common.base.BaseActivity
import com.unionftech.common.base.IUpdate
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.bean.SystemUserInfo
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.router.RouterPath
import com.unionftech.common.utils.ActivityCollector
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.ToastUtils

class ResultOfOpenAccountActivity : BaseActivity(), IUpdate {
    private val mBtnCompleteAuth by lazy { findViewById<Button>(R.id.btn_complete_auth) }
    private val mBtnDepositNow by lazy { findViewById<Button>(R.id.btn_deposit_now) }
    private val mTvResultSubTitle by lazy { findViewById<TextView>(R.id.tv_result_sub_title) }
    private val mTvResultTitle by lazy { findViewById<TextView>(R.id.tv_result_title) }

    companion object {
        @JvmStatic
        fun startThis(context: Context) {
            Intent(context, ResultOfOpenAccountActivity::class.java).apply {
                if (context !is Activity) {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                context.startActivity(this)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_result_of_open_account

    override fun initLanguageText() {
        super.initLanguageText()
        mTvResultTitle.text =
            DynamicResourceManager.getString(mContext, string.application_successful)
        mTvResultSubTitle.text =
            DynamicResourceManager.getString(mContext, string.please_wait_tips)
        mBtnDepositNow.text = DynamicResourceManager.getString(mContext, string.deposit_now)
        mBtnCompleteAuth.text =
            DynamicResourceManager.getString(mContext, string.complete_authentication)
    }

    override fun setListener() {
        super.setListener()
        mBtnDepositNow.setOnClickListener(this)
        mBtnCompleteAuth.setOnClickListener(this)
        GlobalInfoManager.getInstance().addListener(this)
    }

    override fun setView() {
        super.setView()
        initTitleBar()
        loadData()
    }

    override fun loadData() {
        super.loadData()
        getSystemUserInfo()
    }

    private fun getSystemUserInfo() {
        val systemUserInfo = GlobalInfoManager.getInstance().getSystemUserInfo()
        if (systemUserInfo != null) {
            updateResult(systemUserInfo)
        } else {
            GlobalInfoManager.getInstance().update(GlobalInfoManager.UPDATE_USR_INFO)
        }
    }


    private fun updateResult(user: SystemUserInfo) {
        LogUtils.d("ResultOfOpenAccountActivity", "updateResult-->user:${user}")
        val isIb = user.type == UserType.IB.type
        LogUtils.d("ResultOfOpenAccountActivity", "updateResult-->isIb:${isIb}")
        if (isIb) {
            updateIBResult(user)
        } else {
            updateRcResult(user)
        }
    }

    private fun updateRcResult(user: SystemUserInfo) {
        //身份认证没有上传完成
        if (user.status == UserStatus.USER_SUCCESS.status) {
            //账号审核完成，立即入金（展示立即入金和完成身份认证）
            updateCompleteAuthGotoDepositUI()
        } else {
            //展示完成身份认证
            updateCompleteAuthUI()
        }
    }


    private fun updateIBResult(user: SystemUserInfo) {
        //没有交易账号,有在审核中的交易账号(审核中)
        val isAudit = !user.hasAccount && user.hasAudit
        if (!isAudit && user.status == UserStatus.USER_SUCCESS.status) {
            //审核成功
            updateSuccess()

        } else {
            //审核中
            updateWaitingUI()
        }
    }

    private fun updateCompleteAuthGotoDepositUI() {
        mTvResultTitle.text =
            DynamicResourceManager.getString(string.application_successful)
        mTvResultSubTitle.visibility = View.INVISIBLE
        mBtnDepositNow.visibility = View.VISIBLE
        mBtnCompleteAuth.visibility = View.VISIBLE
        mBtnCompleteAuth.setBackgroundResource(drawable.default_grey_button_bg)
        mBtnCompleteAuth.setTextColor(mContext.resources.getColor(color.color_1C1D21))
    }

    private fun updateCompleteAuthUI() {
        mTvResultTitle.text =
            DynamicResourceManager.getString(string.application_successful)
        mTvResultSubTitle.visibility = View.INVISIBLE
        mBtnDepositNow.visibility = View.GONE
        mBtnCompleteAuth.visibility = View.VISIBLE
        mBtnCompleteAuth.setBackgroundResource(drawable.default_button_bg)
        mBtnCompleteAuth.setTextColor(mContext.resources.getColor(color.color_white))
    }

    private fun updateSuccess() {
        mTvResultTitle.text =
            DynamicResourceManager.getString(string.application_successful)
        mTvResultSubTitle.visibility = View.INVISIBLE
        mBtnDepositNow.visibility = View.VISIBLE
        mBtnCompleteAuth.visibility = View.GONE
    }

    private fun updateWaitingUI() {
        mTvResultTitle.text = DynamicResourceManager.getString(string.submit_success)
        mTvResultSubTitle.text = DynamicResourceManager.getString(string.please_wait_tips)
        mTvResultSubTitle.visibility = View.VISIBLE
        mBtnCompleteAuth.visibility = View.GONE
        mBtnDepositNow.visibility = View.GONE
    }

    private fun initTitleBar() {
        hideCenterTitle()
        showBackImg(drawable.title_back)
    }

    override fun backPressed(): Boolean {
        ARouter.getInstance().build(RouterPath.UFUFMainStandardActivityInfo.PATH).navigation()
        finish()
        return true
    }

    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.btn_deposit_now -> {
                //todo:立即入金
                ToastUtils.longToast(
                    DynamicResourceManager.getString(
                        mContext, string.developing
                    )
                )
            }

            R.id.btn_complete_auth -> {
                gotoPoiAuth()
            }
        }
    }

    override fun onUpdate(obj: Any?) {
        if (obj is SystemUserInfo) {
            updateResult(obj)
        } else if (obj is Int) {
            if (obj == GlobalInfoManager.FINISH_SELF) {
                finish()
            }
        }
    }

    private fun gotoPoiAuth() {
        OpenAccountGuideUtil.gotoPoiAuth(
            isSupplement = false,
            isContinuePoa = true
        )
    }

    override fun onDestroy() {
        GlobalInfoManager.getInstance().remove(this)
        super.onDestroy()
    }
}